/*
 * Copyright (c) 2011. c.mueller
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * The Software shall be used for Good, not Evil.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package de.altitude.cimuelle.commandhandles;

import de.altitude.cimuelle.Config;
import de.altitude.cimuelle.commandhandles.customcommands.CommandCoopGame;
import de.altitude.cimuelle.commandhandles.customcommands.MovePlayersToLeft;
import de.altitude.cimuelle.commandhandles.customcommands.ReportAdmin;
import de.altitude.cimuelle.commandhandles.customcommands.ReportUser;
import de.altitudecustomcommands.Player;
import de.altitudecustomcommands.exceptions.ServerNotFoundException;
import de.altitudecustomcommands.playerHandler;
import de.altitudecustomcommands.serverInformations;
import org.apache.log4j.Logger;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Class description:
 * processes consoleCommands
 * User: cybernaut
 * Date: 07.10.11
 * Time: 22:56
 */
public class TypeConsoleHandler {
    private static final Logger LOGGER = Logger.getLogger("TypeConsoleHandler");

    private final playerHandler playerList;
    private final serverInformations server;

    public TypeConsoleHandler(playerHandler playerList, serverInformations server) {
        this.playerList = playerList;
        this.server = server;
    }

    public void typeConsoleCommandExecute(String port, Map json) {
        LOGGER.info(port + ":Console: " + json.toString());
        try {
            Config config = Config.getInstance();
            String vaporId = (String) json.get("source");
            Player player = null;
            if (!vaporId.equals(Player.BOTVAPOR)) {
                player = playerList.getPlayerByVaporID(vaporId);
            }
            if (player == null) {
                player = new Player("unknown", vaporId, "unknown", port, -1);
            }

            @SuppressWarnings({"unchecked"}) List<String> arguments = (LinkedList<String>) json.get("arguments");
            final String command = String.valueOf(json.get("command"));
            if ("reportAdmin".equals(command)) {
                String adminName = arguments.get(0);
                String reason = arguments.get(1);
                new ReportAdmin(server).reportAdmin(port, player, adminName, reason);
            } else if ("reportUser".equals(command)) {
                String playerName = arguments.get(0);
                String reason = arguments.get(1);
                new ReportUser(server).reportUser(port, player, playerName, reason);
            } else if ("goWest".equals(command) || "goWestAdmin".equals(command)) {
                // sends command to server to moves all player to 0
                new MovePlayersToLeft(playerList).moveAllPlayersToLeft(port);
            } else if (config.isCoopRankingActiveByPort(port)) {
                CommandCoopGame commandCoop = new CommandCoopGame(server, playerList);
                if ("coopShowGame".equals(command)) {
                    int coopId = Integer.parseInt(arguments.get(0));
                    commandCoop.showGame(port, player, coopId);
                } else if ("coopTopTenMap".equals(command)) {
                    //String mapName = String.valueOf(arguments.get(0));
                    //commandCoop.coopTopTenMap(port, player, new AltitudeMap(mapName));
                    commandCoop.coopTopTenMap(port, player, server.getServerByPort(port).getMap());
                } else if ("coopTopTenPlayer".equals(command)) {
                    commandCoop.coopTopTenPlayer(port, player);
                } else if ("coopMyRank".equals(command)) {
                    commandCoop.coopGetRank(port, player);
                } else if ("coopPlayerRank".equals(command)) {
                    String playerName = String.valueOf(arguments.get(0));
                    Player cplayer = playerList.getPlayerByName(playerName);
                    commandCoop.coopGetPRank(port, player, cplayer);
                } else if (config.isPlayerCoopAdmin(player)) {
                    // {"name":"coopAdminManageGame","arguments":[["delete"],"int"],"argumentDescriptions":["action", "gameId"],"voteThreshold":100}
                    // {"name":"coopAdminManagePoints","arguments":[["increase","decrease","reset"],["kills","deaths","goals","coop"],"int","String"],"argumentDescriptions":["action", "points to change","amount", "vapor or playername"],"voteThreshold":100}
                    if ("coopAdminManageGame".equals(command) && config.isPlayerCoopManageGameGroup(player)) {
                        // arguments are 0=delete(String) 1=coopId(Integer)
                        String action = String.valueOf(arguments.get(0));
                        int coopId = Integer.parseInt(arguments.get(1));
                        commandCoop.adminManageGame(port, action, coopId, player);
                    } else if ("coopAdminManagePoints".equals(command) && config.isPlayerCoopManagePointsGroup(player)) {
                        // arguments are 0=increase|decrease|reset(String)  1=coop|kill|death|goals(String) 2=points(int) 3=name|vapor(String)
                        String action = String.valueOf(arguments.get(0));
                        String tableColumn = String.valueOf(arguments.get(1));
                        int points = Integer.parseInt(arguments.get(2));
                        String playerArg = String.valueOf(arguments.get(3));
                        commandCoop.adminManagePoints(port, player, action, tableColumn, points, playerArg);
                    } else if ("coopReportBug".equals(command) && config.isPlayerCoopManagePointsGroup(player)) {
                        String bugMessage = String.valueOf(arguments.get(0));
                        commandCoop.reportBug(config, port, player, bugMessage);
                    }
                }
            } else {
                // Dont spam console
                //if (!player.playerIsBot() && !"".equals(player.getIP())) {
                //    ServerOperations.sendServerWhisper(port, player.getName(), "Command not active.");
                //}
            }
        } catch (ServerNotFoundException e) {
            LOGGER.error(port + ":" + e, e);
        }
    }
}
